//
//  DHUser_HomePage_HeadCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHUser_HomePage_HeadCell.h"
#import "QBImagePickerController.h"
#import "DHChangeNicknameController.h"

@interface DHUser_HomePage_HeadCell()<QBImagePickerControllerDelegate>

@end

@implementation DHUser_HomePage_HeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.lineViewWidthLayout.constant = kScreenWidth / 3 - 80;
    self.lineViewLeftLayout.constant = 40;
    self.headImageView.layer.cornerRadius = self.headImageView.height / 2;
    self.headImageView.clipsToBounds = YES;
    self.headImageView.layer.borderWidth = 4;
    self.headImageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    
    self.searchView.layer.cornerRadius = self.searchView.height / 2;
    
    [self.coverImgView bk_whenTapped:^{
        UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册中选择", nil];
        // 显示
        [actionsheet showInView:self.viewController.view];
    }];
}

- (void)updateCell:(NSDictionary *)dic{
    [[GlobalManager shareGlobalManager] refreshBasicInfoAndBlock:^(id data, NSError *error) {
        [self.headImageView dh_setImageURLString:[GlobalManager shareGlobalManager].user.photo];
        self.nameLabel.text = [GlobalManager shareGlobalManager].user.nickname;
        self.disLabel.text = [GlobalManager shareGlobalManager].user.title;
        [self.coverImgView dh_setImageURLString:[GlobalManager shareGlobalManager].user.cover];
    }];
}

- (IBAction)editAction:(id)sender {
    DHChangeNicknameController *vc = [[DHChangeNicknameController alloc] init];
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        
    }else if (buttonIndex == 1){
        QBImagePickerController *imagePickerController = [[QBImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsMultipleSelection = YES;
        imagePickerController.maximumNumberOfSelection = 1;
        imagePickerController.filterType = QBImagePickerControllerFilterTypePhotos;
        UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:imagePickerController];
        [self.viewController presentViewController:navigationController animated:YES completion:NULL];
    }
}

#pragma mark - QBImagePickerControllerDelegate
- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAsset:(ALAsset *)asset{
    [self dismissImagePickerController];
}

- (void)imagePickerControllerDidCancel:(QBImagePickerController *)imagePickerController{
    [self dismissImagePickerController];
}

- (void)imagePickerController:(QBImagePickerController *)imagePickerController didSelectAssets:(NSArray *)assets{
    NSMutableArray *imgs = [NSMutableArray array];
    [assets enumerateObjectsUsingBlock:^(ALAsset *asset, NSUInteger idx, BOOL * _Nonnull stop) {
        ALAssetRepresentation *rep = [asset defaultRepresentation];
        CGImageRef iref = [rep fullScreenImage];
        if (iref) {
            UIImage *img = [UIImage imageWithCGImage:iref];
            [imgs addObject:img];
        }
    }];
    if (imgs.count > 0) {
        [self uploadImage:[imgs objectAtIndex:0]];
    }
    [self dismissImagePickerController];
}

- (void)uploadImage:(UIImage *)img{
    [[GlobalManager shareGlobalManager] upyunUploadImage:img andBlock:^(id data, NSError *error) {
        NSString *url = data;
        [[DHNetAPIClient shareDHNetAPIClient] request_user_coverWithParams:@{@"cover":url} andBlock:^(id data, NSError *error) {
            [self.coverImgView dh_setImageURLString:url];
        }];
    }];
}

- (void)dismissImagePickerController{
    [self.viewController dismissViewControllerAnimated:YES completion:NULL];
}

- (IBAction)tabAction:(id)sender {
    UIButton *btn = (UIButton *)sender;
    self.lineViewLeftLayout.constant = btn.left + 40;
    switch (btn.tag) {
        case 1000:{
            [self routerEventWithName:@"tab_1" userInfo:nil];
            break;
        }
        case 1001:{
            [self routerEventWithName:@"tab_2" userInfo:nil];
            break;
        }
        case 1002:{
            [self routerEventWithName:@"tab_3" userInfo:nil];
            break;
        }
            
        default:
            break;
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
